<?php

namespace app\api\service\client_login;

use app\common\exception\CustomException;
use app\common\service\HostModuleRelation;

use app\common\service\HostLink;
use app\common\service\platform_manage\ServerProxyService;
use think\Db;

/**
 * @name: 客户端登陆
 * @Param: 
 * @Description: 
 * @Author: foo
 * @Date: 2019-12-07 14:05:58
 */

class LoginService
{

    public $host_os = 0;//主机系统
    public $host_id = 0;//主机id
    public $poxy_ip = '';//代理主机ip
    public $poxy_port = 0;//模块主机端口
    public $module_ip = '';//代理主机ip
    public $module_port = 0;//模块端口

    /**
     * @name: 数据返回
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-09 14:24:27
     */
    public function requestData ($data)
    {

        $this->getHostId($data['hostkey']);
        if(!$this->checkModule($this->module))
        {
            return [];
        }

        $this->getModuleConnect($this->module);
        $this->getPoxyConnect();

        $return = [
            'modules_name' => $this->module,
            'host_id' => $this->host_id,
            'proxy_ip' => $this->poxy_ip,
            'proxy_port' => (int) $this->poxy_port,
            'modules_ip' => $this->module_ip,
            'modules_port' => (int) $this->module_port,
        ];

        $return['config'] = $this->login($data);

        $this->updateVersion($this->module, $data['version']);//更新 客户端/模块 版本


        return $return;

    }

    /**
     * @name: 获取模块连接
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-09 13:50:35
     */
    private function getModuleConnect($module)
    {
        $ServerProxy = new ServerProxyService();

        $return = $ServerProxy->getIp($module);
        if (empty($return))
        {
            return false;
        }

        $this->module_ip = $return['ip'];
        $this->module_port = $return['port'];
    }

    /**
     * @name: 获取虚拟ip
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-09 13:58:05
     */
    private function getPoxyConnect()
    {

        $ServerProxyService = new ServerProxyService();
        $return = $ServerProxyService->getIp('proxy');

        if (empty($return))
        {
            return false;
        }

        $this->poxy_ip = $return['ip'];
        $this->poxy_port = $return['port'];
    }

    /**
     * @name: 获取主机id
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-09 14:09:23
     */
    private function getHostId ($hostkey)
    {

        $host = Db::name('Host')->where('hostkey', $hostkey)->find();
        if(empty($host))
        {
            throw new CustomException('不存在该主机', 422);
        }
        $this->host_os = $host['os'];
        $this->host_id = $host['id'];
    }

    /**
     * @name: 检测模块状态
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2020-01-04 11:15:19
     */
    private function checkModule ($module)
    {
        $HostModuleRelation = new HostModuleRelation();

        $hostModule = $HostModuleRelation->findByHostId($this->host_id);

        if(empty($hostModule))
        {
            throw new CustomException('不存在该主机', 422);
        }


        if(!isset($hostModule[$module]))
        {
            throw new CustomException('该模块不存在', 422);
        }

        if($hostModule[$module] == 0)
        {
            throw new CustomException('该模块未开启', 422);
        }

        return true;
    }

    /**
     * @name: 更新版本号
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2020-01-07 19:03:59
     */
    public function updateVersion ($module, $version)
    {
        
        $HostLink = new HostLink;
        $HostLink->updateVersion($this->host_id, $module, $version);
    }
}